package com.itheima.web;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.Service.DishService;
import com.itheima.bean.Dish;
import com.itheima.common.R;
import com.itheima.dto.DishDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.yaml.snakeyaml.events.Event;

import java.util.List;


@RequestMapping("/dish")
@RestController
public class DishController {

     @Autowired
    private DishService dishService ;

    /**
     * 根据菜品种类id与菜品查询菜品
     * @param categoryId
     * @param status
     * @return
     */
      @GetMapping("/list")
     public R FindByCategoryId(long categoryId,int status){


          List<DishDto> dishDtoList = dishService.findByCategoryId(categoryId,status);

          return R.success(dishDtoList);

     }

    /**
     * 分页查询菜品+模糊查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
     @GetMapping("/page")
     public R FindPage(int page,int pageSize,String name){
/*         System.out.println("page = " + page);
         System.out.println("pageSize = " + pageSize);*/
         Page<DishDto> page1 = dishService.findPage(page, pageSize, name);
         if (page1!=null){
             return R.success(page1);
         }
         return R.error("查询失败");
     }

    /**
     * 新增菜品
     * @param dishDto
     * @return
     */
     @PostMapping
     public R AddCategory(@RequestBody DishDto dishDto){
//         System.out.println("dishDto = " + dishDto);
         boolean b = dishService.addDish(dishDto);
         if (b){
             return R.success("新增成功");
         }
         return R.error("新增失败");
     }

    /**
     * 根据Id查询菜品的内容
     * @param id
     * @return
     */
     @GetMapping("/{id}")
     public R FindCategoryById(@PathVariable Long id){
//         System.out.println("id = " + id);
         DishDto dto = dishService.findById(id);
         if (dto!=null){
             return R.success(dto);
         }
         return R.error("查询不到该菜品信息");
     }

    /**
     * 菜品销售状态修改
     * @param status
     * @param ids
     * @return
     */
     @PostMapping("/status/{status}")
     public R updateStatus(@PathVariable int status, Long[] ids){
/*         System.out.println("status = " + status);
         System.out.println("ids = " + ids);*/
         boolean b = dishService.updateStatus(ids,status);
         if (b){
             return R.success("状态修改成功");
         }
         return R.error("状态修改失败");
     }

    /**
     * 菜品更新
     * @param dishDto
     * @return
     */
     @PutMapping
     public R updateDish(@RequestBody DishDto dishDto){
//         System.out.println("dishDto = " + dishDto);
         boolean b = dishService.updateDish(dishDto);
         if (b){
             return R.success("菜品表更新成功");
         }
         return R.error("菜品表更新失败");
     }

     @DeleteMapping
     public R deleteDish(Long[] ids){
//         System.out.println("ids = " + ids);
         int b = dishService.deleteByIds(ids);
         if (b==-1){
             return R.error("菜品包含在其他套餐中无法删除");
         }else if (b==0){
             return R.success("删除菜品成功");

         }
         return R.error("删除菜品成功");
     }
}
